home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevmsxf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.7 KB  |  469 lines

  1. /* Copyright (C) 1993, 1994, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevmsxf.c,v 1.2 2000/09/19 19:00:14 lpd Exp $ */
  20. /* External font (xfont) implementation for Microsoft Windows. */
  21. #include "ctype_.h"
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "string_.h"
  25. #include "gdevmswn.h"
  26. #include "gsutil.h"
  27. #include "gxxfont.h"
  28. #include "gsstruct.h"
  29.  
  30. /* Imported from gdevemap.c */
  31. extern const byte gs_map_std_to_iso[256];
  32.  
  33. /* Declare the xfont procedures */
  34. private xfont_proc_lookup_font(win_lookup_font);
  35. private xfont_proc_char_xglyph(win_char_xglyph);
  36. private xfont_proc_char_metrics(win_char_metrics);
  37. private xfont_proc_render_char(win_render_char);
  38. private xfont_proc_release(win_release);
  39. private const gx_xfont_procs win_xfont_procs =
  40. {
  41.     win_lookup_font,
  42.     win_char_xglyph,
  43.     win_char_metrics,
  44.     win_render_char,
  45.     win_release
  46. };
  47.  
  48. /* Return the xfont procedure record. */
  49. const gx_xfont_procs *
  50. win_get_xfont_procs(gx_device * dev)
  51. {
  52.     return &win_xfont_procs;
  53. }
  54.  
  55. /* Define a Windows xfont. */
  56. typedef struct win_xfont_s win_xfont;
  57. struct win_xfont_s {
  58.     gx_xfont_common common;
  59.     LOGFONT lf;
  60.     TEXTMETRIC tm;
  61.     HFONT hFont;
  62.     gx_device_win *dev;        /* for GetDC */
  63.     int invert_y;
  64.     int y_offset;
  65. };
  66.  
  67. gs_private_st_dev_ptrs1(st_win_xfont, win_xfont, "win_xfont",
  68.             win_xfont_enum_ptrs, win_xfont_reloc_ptrs, dev);
  69. #define wxf ((win_xfont *)xf)
  70.  
  71. /* Forward references */
  72. private HDC near win_get_dc(P1(gx_device_win *));
  73. private void near win_release_dc(P2(gx_device_win *, HDC));
  74. private int win_select_font(P2(HDC, win_xfont *));
  75.  
  76. /* Map from PostScript to Windows character codes. */
  77. /* (These tables were generated by winmaps.ps.) */
  78.  
  79. private const byte far_data gs_map_symbol_to_oem[256] =
  80. {
  81.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  82.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83.     32, 33, 0, 35, 0, 37, 38, 0, 40, 41, 0, 43, 44, 0, 46, 47,
  84.     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  85.     0, 0, 0, 0, 0, 0, 231, 225, 0, 0, 0, 0, 0, 0, 0, 0,
  86.     226, 232, 0, 227, 0, 0, 0, 233, 0, 0, 0, 91, 0, 93, 0, 95,
  87.     0, 223, 224, 0, 234, 0, 236, 0, 0, 0, 0, 0, 0, 229, 0, 0,
  88.     0, 0, 0, 228, 230, 0, 0, 0, 0, 0, 0, 123, 124, 125, 0, 0,
  89.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91.     0, 0, 0, 242, 0, 235, 0, 5, 4, 3, 6, 29, 27, 24, 26, 25,
  92.     247, 240, 0, 241, 0, 0, 0, 7, 245, 0, 239, 0, 0, 0, 0, 0,
  93.     0, 0, 0, 0, 0, 0, 0, 238, 0, 0, 0, 0, 0, 0, 237, 0,
  94.     0, 0, 0, 0, 0, 0, 250, 248, 169, 0, 0, 0, 0, 0, 0, 0,
  95.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96.     0, 0, 0, 243, 0, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  97. };
  98.  
  99. private const byte far_data gs_map_iso_to_oem[256] =
  100. {
  101.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103.     32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 0, 46, 47,
  104.     48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  105.     64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  106.     80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  107.  96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  108. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 0,
  109.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  110.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111.     32, 172, 154, 155, 0, 156, 0, 21, 0, 0, 165, 173, 169, 45, 0, 0,
  112.     247, 240, 252, 0, 0, 229, 20, 249, 0, 0, 166, 174, 171, 170, 0, 167,
  113.     0, 0, 0, 0, 141, 142, 145, 128, 0, 143, 0, 0, 0, 0, 0, 0,
  114.     0, 164, 0, 0, 0, 0, 152, 0, 0, 0, 0, 0, 153, 0, 0, 0,
  115.  133, 159, 131, 0, 132, 134, 144, 135, 138, 130, 136, 137, 140, 160, 0, 139,
  116.     0, 163, 148, 161, 146, 0, 147, 245, 0, 150, 162, 149, 129, 0, 0, 151
  117. };
  118.  
  119. /* Correlate PostScript font names with Windows font names. */
  120. /* This table should be an external resource, like Fontmap, */
  121. /* but that will have to wait till later. */
  122.  
  123. typedef struct font_entry_s {
  124.     const char *key;
  125.     const char *value;
  126.     uint pitchAndFamily;
  127. } font_entry;
  128.  
  129. private const font_entry font_names[] =
  130. {
  131.     {"Courier", "Courier New", FIXED_PITCH | FF_MODERN},
  132.     {"Helvetica", "Arial", VARIABLE_PITCH | FF_SWISS},
  133.     {"Helvetica", "Helv", VARIABLE_PITCH | FF_SWISS},
  134.     {"Times", "Times New Roman", VARIABLE_PITCH | FF_ROMAN},
  135.     {"Times", "Tms Rmn", VARIABLE_PITCH | FF_ROMAN}
  136. };
  137.  
  138. /* Look up a font. */
  139. private int /*bool */ map_logical_font(P2(HDC, win_xfont *));
  140. gx_xfont *
  141. win_lookup_font(gx_device * dev, const byte * fname, uint len,
  142.         int encoding_index, const gs_uid * puid, const gs_matrix * pmat,
  143.         gs_memory_t * mem)
  144. {
  145.     win_xfont f;
  146.     win_xfont *wf;
  147.     uint name_len = min(len, LF_FACESIZE - 1);
  148.     const font_entry *pfe;
  149.     HDC hdc;
  150.  
  151.     /* Only handle simple cases for now. */
  152.     if (pmat->xy != 0 || pmat->yx != 0 || pmat->xx <= 0 ||
  153.     fabs(fabs(pmat->yy) - pmat->xx) > 0.00002
  154.     )
  155.     return NULL;
  156.     f.lf.lfHeight = pmat->xx * 1000;
  157.     /* Don't trust Windows with very small sizes. */
  158.     if (f.lf.lfHeight < 6 || f.lf.lfHeight >= 36)
  159.     return NULL;
  160.     f.lf.lfWidth = 0;
  161.     f.lf.lfEscapement = 0;
  162.     f.lf.lfOrientation = 0;
  163.     f.lf.lfWeight =
  164.     (string_match(fname, len, "*Bold*", 6, NULL) ?
  165.      FW_BOLD : FW_REGULAR);
  166.     f.lf.lfItalic =
  167.     string_match(fname, len, "*Italic*", 8, NULL) ||
  168.     string_match(fname, len, "*Oblique*", 9, NULL);
  169.     f.lf.lfUnderline = 0;
  170.     f.lf.lfStrikeOut = 0;
  171.     f.lf.lfCharSet =
  172.     (encoding_index == 2 ? SYMBOL_CHARSET : ANSI_CHARSET);
  173.     f.lf.lfOutPrecision = OUT_CHARACTER_PRECIS;
  174.     f.lf.lfClipPrecision = CLIP_STROKE_PRECIS;
  175.     f.lf.lfQuality = PROOF_QUALITY;
  176.     f.hFont = 0;
  177.     f.invert_y = pmat->yy >= 0;
  178.     hdc = win_get_dc(wdev);
  179.     if (hdc == NULL)
  180.     return NULL;
  181.     for (pfe = font_names; pfe != &font_names[countof(font_names)]; pfe++)
  182.     if (!strncmp(pfe->key, fname, strlen(pfe->key))) {    /* Found a match. */
  183.         strcpy(f.lf.lfFaceName, pfe->value);
  184.         f.lf.lfPitchAndFamily = pfe->pitchAndFamily;
  185.         if (map_logical_font(hdc, &f))
  186.         break;
  187.     }
  188.     if (f.hFont == 0) {        /* No matches in the table, try with the given name. */
  189.     uint len;
  190.  
  191.     memcpy(f.lf.lfFaceName, fname, name_len);    /* default */
  192.     for (len = 0; len < name_len; len++)
  193.         if (!isalnum(fname[len]))
  194.         break;
  195.     f.lf.lfFaceName[len] = 0;
  196.     f.lf.lfPitchAndFamily = 0;    /* default */
  197.     if (!map_logical_font(hdc, &f)) {
  198.         win_release_dc(wdev, hdc);
  199.         return NULL;
  200.     }
  201.     }
  202.     GetTextMetrics(hdc, &f.tm);
  203.     win_release_dc(wdev, hdc);
  204.     f.y_offset = (!f.invert_y ? f.tm.tmAscent : f.tm.tmDescent);
  205.     wf = gs_alloc_struct(mem, win_xfont, &st_win_xfont, "win_lookup_font");
  206.     if (wf == 0) {
  207.     DeleteObject(f.hFont);
  208.     return NULL;
  209.     }
  210.     f.common.procs = &win_xfont_procs;
  211.     f.dev = wdev;
  212.     *wf = f;
  213.     return (gx_xfont *) wf;
  214. }
  215. /* Map the logical font, and see if the result is satisfactory. */
  216. private int            /*bool */
  217. map_logical_font(HDC hdc, win_xfont * xf)
  218. {
  219.     char szFaceName[LF_FACESIZE];
  220.  
  221.     xf->hFont = CreateFontIndirect(&xf->lf);
  222.     if (xf->hFont == 0)
  223.     return 0;
  224.     /* Check the face name */
  225.     SelectObject(hdc, xf->hFont);
  226.     GetTextFace(hdc, sizeof(szFaceName), szFaceName);
  227.     if (!strncmp(xf->lf.lfFaceName, szFaceName, strlen(xf->lf.lfFaceName)))
  228.     return 1;
  229.     DeleteObject(xf->hFont);
  230.     xf->hFont = 0;
  231.     return 0;
  232. }
  233.  
  234. /* Convert a character name or index to an xglyph code. */
  235. gx_xglyph
  236. win_char_xglyph(gx_xfont * xf, gs_char chr, int encoding_index,
  237.         gs_glyph glyph, gs_proc_glyph_name_t glyph_name_proc)
  238. {
  239.     if (chr == gs_no_char)
  240.     return gx_no_xglyph;    /* can't look up names yet */
  241.     if (encoding_index == 0) {    /* Map StandardEncoding to ISOLatin1Encoding. */
  242.     /* We lose a couple of characters that exist in both */
  243.     /* StandardEncoding and the Windows OEM encoding but not in */
  244.     /* the ISOLatin1Encoding; we won't worry about this */
  245.     /* for now. */
  246.     chr = gs_map_std_to_iso[chr];
  247.     encoding_index = 1;
  248.     }
  249.     if (wxf->hFont == NULL) {    /* TEXTMETRICS not filled in yet */
  250.     HDC hdc = win_get_dc(wxf->dev);
  251.     int code;
  252.  
  253.     if (hdc == NULL)
  254.         return gx_no_xglyph;
  255.     code = win_select_font(hdc, wxf);
  256.     win_release_dc(wxf->dev, hdc);
  257.     if (code < 0)
  258.         return gx_no_xglyph;
  259.     }
  260.     switch (wxf->tm.tmCharSet) {
  261.     case ANSI_CHARSET:
  262.         if (encoding_index == 1 && (chr < 0x7f || chr > 0x9f ||
  263.                     chr == 0x91 || chr == 0x92)
  264.         )
  265.         break;
  266.         return gx_no_xglyph;
  267.     case OEM_CHARSET:
  268.         switch (encoding_index) {
  269.         case 1:    /* ISOLatin1 */
  270.             chr = gs_map_iso_to_oem[chr];
  271.             break;
  272.         case 2:    /* Symbol */
  273.             chr = gs_map_symbol_to_oem[chr];
  274.             break;
  275.         default:
  276.             return gx_no_xglyph;
  277.         }
  278.         break;
  279.     default:
  280.         return gx_no_xglyph;
  281.     }
  282.     return (chr != 0 && chr >= wxf->tm.tmFirstChar &&
  283.         chr <= wxf->tm.tmLastChar ?
  284.         (gx_xglyph) chr : gx_no_xglyph);
  285. }
  286.  
  287. /* Get the metrics for a character. */
  288. int
  289. win_char_metrics(gx_xfont * xf, gx_xglyph xg, int wmode,
  290.          gs_point * pwidth, gs_int_rect * pbbox)
  291. {
  292.     int code;
  293.     HDC hdc;
  294.     char chr = (char)xg;
  295.  
  296.     if (wmode != 0)
  297.     return gs_error_undefined;
  298.     hdc = win_get_dc(wxf->dev);
  299.     if (hdc == NULL)
  300.     return gs_error_limitcheck;
  301.     if ((code = win_select_font(hdc, wxf)) < 0) {
  302.     win_release_dc(wxf->dev, hdc);
  303.     return code;
  304.     }
  305. #ifdef __WIN32__
  306.     {
  307.     SIZE sz;
  308.  
  309.     GetTextExtentPoint(hdc, &chr, 1, &sz);
  310.     pwidth->x = sz.cx;
  311.     }
  312. #else
  313.     {
  314.     DWORD extent;
  315.  
  316.     extent = GetTextExtent(hdc, &chr, 1);
  317.     pwidth->x = LOWORD(extent);
  318.     }
  319. #endif
  320.     win_release_dc(wxf->dev, hdc);
  321.     pwidth->y = 0;
  322.     pbbox->p.x = 0;
  323.     pbbox->q.x = pwidth->x;
  324.     if (wxf->invert_y) {
  325.     pbbox->p.y = -wxf->tm.tmDescent;
  326.     pbbox->q.y = wxf->tm.tmAscent;
  327.     } else {
  328.     pbbox->p.y = -wxf->tm.tmAscent;
  329.     pbbox->q.y = wxf->tm.tmDescent;
  330.     }
  331.     return 0;
  332. }
  333.  
  334. /* Render a character. */
  335. int
  336. win_render_char(gx_xfont * xf, gx_xglyph xg, gx_device * dev,
  337.         int xo, int yo, gx_color_index color, int required)
  338. {
  339.     char chr = (char)xg;
  340.     int code;
  341.  
  342. #ifdef NOTUSED            /* we don't own any windows so we can no longer do this */
  343.     if (dev->dname == gs_mswin_device.dname &&
  344.     wdev->hdctext != NULL && !wxf->invert_y
  345.     ) {            /* Display the character directly */
  346.     HDC hdc = wdev->hdctext;
  347.     PALETTEENTRY *pal = &wdev->limgpalette->palPalEntry[color];
  348.  
  349.     if ((code = win_select_font(hdc, wxf)) < 0)
  350.         return code;
  351.     SetTextColor(hdc, RGB(pal->peRed, pal->peGreen, pal->peBlue));
  352.     SetBkMode(hdc, TRANSPARENT);
  353.     TextOut(hdc, xo, yo - wxf->y_offset, &chr, 1);
  354.     } else
  355. #endif
  356.     if (!required)
  357.     code = -1;        /* too hard */
  358.     else {            /* Display on an intermediate bitmap, then copy the bits. */
  359.     gs_point wxy;
  360.     gs_int_rect bbox;
  361.     int w, h, wbm, raster;
  362.     gx_device_win *fdev = wxf->dev;
  363.     HBITMAP hbm;
  364.     byte *bits;
  365.  
  366.     code = (*xf->common.procs->char_metrics) (xf, xg, 0,
  367.                           &wxy, &bbox);
  368.     if (code < 0)
  369.         return code;
  370.     w = bbox.q.x - bbox.p.x;
  371.     h = bbox.q.y - bbox.p.y;
  372.     wbm = ROUND_UP(w, align_bitmap_mod * 8);
  373.     raster = wbm >> 3;
  374.     bits = gs_malloc(h, raster, "win_render_char");
  375.     if (bits == 0)
  376.         return gs_error_limitcheck;
  377.     hbm = CreateBitmap(wbm, h, 1, 1, NULL);
  378.     if (hbm == NULL) {
  379.         code = gs_error_limitcheck;
  380.     } else {
  381.         HDC hdcwin = win_get_dc(fdev);
  382.         HDC hdcbit = CreateCompatibleDC(hdcwin);
  383.  
  384.         dev_proc_copy_mono((*copy_mono)) =
  385.         dev_proc(dev, copy_mono);
  386.         int y = yo - wxf->y_offset;
  387.  
  388.         SetMapMode(hdcbit, GetMapMode(hdcwin));
  389.         win_select_font(hdcbit, wxf);
  390.         SelectObject(hdcbit, hbm);
  391.         PatBlt(hdcbit, 0, 0, wbm, h, rop_write_0s);
  392.         SetTextColor(hdcbit, 0xffffffL);    /* 1 */
  393.         SetBkMode(hdcbit, TRANSPARENT);
  394.         TextOut(hdcbit, 0, 0, &chr, 1);
  395.         GetBitmapBits(hbm, (DWORD) raster * h, bits);
  396.         DeleteDC(hdcbit);
  397.         win_release_dc(fdev, hdcwin);
  398.         DeleteObject(hbm);
  399.         if (!wxf->invert_y)
  400.         code = (*copy_mono) (dev, bits, 0,
  401.                      raster, gx_no_bitmap_id,
  402.                      xo, y, w, h,
  403.                      gx_no_color_index, color);
  404.         else {        /* Copy scan lines in reverse order. */
  405.         int i;
  406.  
  407.         y += h - 1;
  408.         for (i = 0; i < h; i++)
  409.             (*copy_mono) (dev, bits + i * raster,
  410.                   0, raster, gx_no_bitmap_id,
  411.                   xo, y - i, w, 1,
  412.                   gx_no_color_index, color);
  413.         }
  414.     }
  415.     gs_free(bits, h, raster, "win_render_char");
  416.     }
  417.     return (code < 0 ? code : 0);
  418. }
  419.  
  420. /* Release an xfont. */
  421. private int
  422. win_release(gx_xfont * xf, gs_memory_t * mem)
  423. {
  424.     if (wxf->hFont) {
  425.     DeleteObject(wxf->hFont);
  426.     wxf->hFont = 0;
  427.     }
  428.     if (mem != NULL)
  429.     gs_free_object(mem, xf, "win_release");
  430.     return 0;
  431. }
  432.  
  433. /* ------ Font utilities ------ */
  434.  
  435. #undef wdev
  436. #undef wxf
  437.  
  438. /* Get a DC for the font's device. */
  439. private HDC near
  440. win_get_dc(gx_device_win * wdev)
  441. {
  442.     /* Since we don't have a window, use the desktop */
  443.     /* Don't draw into it! */
  444.     return GetDC(HWND_DESKTOP);
  445. }
  446.  
  447. /* Release a DC for the font's device. */
  448. private void near
  449. win_release_dc(gx_device_win * wdev, HDC hdc)
  450. {
  451.     ReleaseDC(HWND_DESKTOP, hdc);
  452. }
  453.  
  454. /* Make an xfont current, possibly remapping it from a logical font. */
  455. private int
  456. win_select_font(HDC hdc, win_xfont * wxf)
  457. {
  458.     HFONT hFont = wxf->hFont;
  459.  
  460.     if (hFont == NULL) {    /* The font was released to free up resources. */
  461.     /* Re-acquire it now. */
  462.     wxf->hFont = CreateFontIndirect(&wxf->lf);
  463.     if (wxf->hFont == NULL)
  464.         return gs_error_limitcheck;
  465.     }
  466.     SelectObject(hdc, wxf->hFont);
  467.     return 0;
  468. }
  469.